package leetcode.t5Stack;

import java.util.Stack;

/**
 * @author K
 * @date 2024/1/20 17:33
 */
public class T71_SimplifyPath {
    public static String simplifyPath(String path) {
        Stack<String> stack = new Stack<String>();
        for (String s : path.split("/")) {
            if (s.equals("..")) {
                if (!stack.isEmpty()) stack.pop();
            } else {
                if (!s.isEmpty() && !s.equals(".")) stack.push(s);
            }
        }

        String ans = "";
        while (!stack.isEmpty()) {
            ans = "/" + stack.pop() + ans;
        }

        return ans.isEmpty() ? "/" : ans;
    }

    public static void main(String[] args) {
        String s = simplifyPath("/home/");
        String s1 = simplifyPath("/../");
        String s2 = simplifyPath("/home//foo/");
        String s3 = simplifyPath("/a/./b/../../c/");
        System.out.println();
    }
}
